let parentMapKeyByChild = new Map<Node, Node>()

function getRootEl(el: Node) {
    let _p: Node = el
    while (true) {
        const parentNode = _p.parentNode
        if (parentNode === null || parentNode === window.document.body) {
            return _p
        }
        _p = parentNode
    }
}

export default {
    logLayerRelation(parent: Node, child: Node) {
        parentMapKeyByChild.set(getRootEl(child), getRootEl(parent))
    },
    closeLayer(child: Node) {
        parentMapKeyByChild.delete(getRootEl(child))
    },
    isOpenedByMeOrMyChildren(parent: Node, el: Node) {
        let parentRootEl = getRootEl(parent)
        let _p = parentMapKeyByChild.get(getRootEl(el))
        while (_p) {
            if (_p === parentRootEl) {
                return true
            }
            _p = parentMapKeyByChild.get(_p)
        }
        return false
    }
}